home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************************************
- * PPP-Prefs.c *
- * *
- * These routines were designed to handle the preferences for MacPPP. *
- * Written by Tony Andreoli, 12/5/95 *
- * *
- * You may use these routines freely, I only ask that you credit me somewhere in your *
- * code. *
- * *
- * Notes: *
- * • HandleError is just a simple error handler. *
- * • OpenPPPPrefs simply opens the PPP Preferences file, and returns the fRef. *
- * • MacPPP considers the PREF the first part of the data fork, which consists of *
- * settings that do not change across sets. It considers a CONFIG those settings *
- * specific to a set. *
- * • The config index starts at 0, yet thePref.max_config starts at 1. Thus if *
- * thePref.max_config=4, the last config is 3 (0..3). *
- * • Be sure to look in the PPP-Prefs.h file for the structure of the prefs and *
- * config. *
- * • Each routine opens then closes the PPP Preferences file. It is NOT left open *
- * upon return. *
- * *
- * Purpose: *
- * GetPPPPref - Gets the PPP preferences. *
- * GetPPPConfig - Gets a specified config set. *
- * PutPPPPref - Writes PPP preferences. *
- * PutPPPConfig - Writes config to a specified set. *
- * DeletePPPConfig - Deletes a specified (indexed) config, decrements max_config. *
- * AddPPPConfig - Adds config to end of data fork, increments max_config. *
- * *
- * The following code is pretty worthless, but it does show some examples of useage. *
- * *
- * Example: *
- * struct ppp_pref thePref; *
- * struct ppp_config theConfig; *
- * *
- * GetPPPPref(&thePref); *
- * GetPPPConfig(&theConfig,thePref.active_config); *
- * ParamText(thePref.portname,theConfig.config_name,theConfig.commands[1].scriptstr,"\p"); *
- * Alert(128,nil); *
- * *
- * strcpy((char *)thePref.portname,(char *)"\pPrinter-Modem Port"); *
- * strcpy((char *)theConfig.config_name,(char *)"\pDidIMakeIt2?"); *
- * PutPPPPref(thePref); *
- * PutPPPConfig(theConfig,1); *
- * *
- * DeletePPPConfig(2); *
- * AddPPPConfig(theConfig); *
- ********************************************************************************************/
-
- #include "PPP-Prefs.h"
-
- #define thePrefsFile "\pPPP Preferences"
-
- void HandleError(int errNum, Str255 errStr)
- {
- Str255 theErr;
-
- if (errNum!=noErr) {
- NumToString(errNum,theErr);
- ParamText(theErr,errStr,"\p","\p");
- Alert(128,nil);
- ExitToShell();
- }
- }
-
- short OpenPPPPrefs(void)
- {
- OSErr err;
- short foundvRefNum;
- long foundDirID;
- short SystemFolderID;
- short theResFile;
- FInfo thefInfo;
-
- err=FindFolder(kOnSystemDisk,kPreferencesFolderType,kDontCreateFolder,&foundvRefNum,&foundDirID); /* Get location of the preferences folder */
- err = HOpen(foundvRefNum,foundDirID,thePrefsFile,fsRdWrShPerm,&theResFile);
- if (err != noErr) {
- HandleError(err,"\pOpenPrefsFile:OpenResFile");
- }
- return theResFile;
- }
-
- void GetPPPPref(struct ppp_pref *thePref)
- {
- long count;
- OSErr rc;
- short prefref;
- Str255 theStr;
-
- prefref=OpenPPPPrefs();
- rc = SetFPos(prefref,fsFromStart,0);
- count = sizeof ( struct ppp_pref );
- if ( rc == noErr) {
- rc = FSRead(prefref, &count, thePref);
- }
- else
- HandleError(rc,"\pSetFPos for PPP Prefs");
- FSClose(prefref);
- }
-
- void GetPPPConfig(struct ppp_config *theConfig,short confignum)
- {
- long count;
- OSErr rc;
- short prefref;
-
- prefref=OpenPPPPrefs();
- rc = SetFPos(prefref,fsFromStart,sizeof (struct ppp_pref)+(sizeof (struct ppp_config) * confignum));
- count = sizeof ( struct ppp_config );
- if ( rc == noErr) {
- rc = FSRead(prefref, &count, theConfig);
- }
- else
- HandleError(rc,"\pSetFPos for PPP Config");
- FSClose(prefref);
-
- }
-
- void PutPPPPref(struct ppp_pref thePref)
- {
- long count;
- OSErr rc;
- short prefref;
- Str255 theStr;
-
- prefref=OpenPPPPrefs();
- rc = SetFPos(prefref,fsFromStart,0);
- count = sizeof ( struct ppp_pref );
- if ( rc == noErr) {
- rc = FSWrite(prefref, &count, &thePref);
- }
- else
- HandleError(rc,"\pSetFPos for PPP Prefs");
- FSClose(prefref);
- }
-
- void PutPPPConfig(struct ppp_config theConfig,short confignum)
- {
- long count;
- OSErr rc;
- short prefref;
-
- prefref=OpenPPPPrefs();
- rc = SetFPos(prefref,fsFromStart,sizeof (struct ppp_pref)+(sizeof (struct ppp_config) * confignum));
- count = sizeof ( struct ppp_config );
- if ( rc == noErr) {
- rc = FSWrite(prefref, &count, &theConfig);
- }
- else
- HandleError(rc,"\pSetFPos for PPP Config");
- FSClose(prefref);
-
- }
-
- void DeletePPPConfig(short confignum)
- {
- struct ppp_pref thePref;
- struct ppp_config theConfig;
- short loop;
- OSErr err;
- short prefref;
-
- GetPPPPref(&thePref);
- if (confignum <= (thePref.max_config-1)) {
- for (loop=confignum;loop<=(thePref.max_config-1);loop++) {
- GetPPPConfig(&theConfig,loop+1);
- PutPPPConfig(theConfig,loop);
- }
- thePref.max_config--;
- if (thePref.active_config>(thePref.max_config-1))
- thePref.active_config=thePref.max_config-1;
- PutPPPPref(thePref);
- prefref=OpenPPPPrefs();
- err = SetEOF(prefref,sizeof (struct ppp_pref)+(sizeof (struct ppp_config) * thePref.max_config));
- FSClose(prefref);
- }
- }
-
- void AddPPPConfig(struct ppp_config theConfig)
- {
- struct ppp_pref thePref;
- short loop;
- OSErr err;
- short prefref;
-
- GetPPPPref(&thePref);
- thePref.max_config++;
- PutPPPConfig(theConfig,thePref.max_config-1);
- PutPPPPref(thePref);
- }